

import store from './vuex/index' // 获取状态管理
import { SET_USER_INFO, SET_EXIST_INFO } from './vuex/type.js'
import Vue from 'vue'
import router from './router'
// 引入axios文件
import axios from 'axios';
const globalConfig = {
  // appid
  appid: 'wx9d1880dc43cb7bf2',
  // 请求地址
  requireUrl: 'http://ymx.sailafeina.com/',
  validation: 'ymx',
  companyUnique: 'ymxWxUserInfo',
  // weburl
  webUrl: 'http://ymxwap.sailafeina.com/',
  // 获取浏览器信息
  browerInfo: window.navigator.userAgent.toLowerCase(),
  // 获取缓存用户信息
  getlocalStorage: ((storeName) => {
    var storeName = localStorage.getItem(storeName);
    var storeArr = (storeName && storeName != 'undefined') ? JSON.parse(storeName) : ''
    return storeArr;
  }),
  // 获取浏览器url信息
  getUrlParams: ((name) => {
    var _reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"),
      _regNext = window.location.search.substr(1).match(_reg);
    if (_regNext != null) return decodeURI(_regNext[2]) || '';
    else return '';
  }),
  // 清除时间
  closeTimesTRamp: ((timestamp) => {
    if (new Date(timestamp).toDateString() !== new Date().toDateString()) {
      globalConfig.setlocalStorage('userInfoTimesTamp', new Date().getTime())
      window.localStorage.removeItem(Vue.prototype.companyUnique)
    }
  }),
  // 设置缓存信息
  setlocalStorage: ((storeName, storeData) => {
    var storeStr = JSON.stringify(storeData);
    var storeArr = window.localStorage.setItem(storeName, storeStr);
    return storeArr;
  }),

  // 微信授权
  fnHandleWxLogin: ((code) => {
    axios.get(`${globalConfig.requireUrl}user/v1/wxlogin/?code=${code}&state=${globalConfig.validation}`)
      .catch(res => {
        if (res.data && res.data.id) {
          store.commit(SET_USER_INFO, res.data)
          setTimeout(() => {
            store.commit(SET_EXIST_INFO, true)
          }, 500)
          globalConfig.setlocalStorage(Vue.prototype.companyUnique, res.data)
        }
      })
  }),

  // 调用后台微信授权接口
  fnGetWxUserInfo: ((openid, id) => {
    axios.get(`${globalConfig.requireUrl}user/v1/newuserinfo/?openid=${openid}`)
      .then(function (res) {
      })
      .catch(res => {
        if (res.data.openid) {
          store.commit(SET_USER_INFO, res.data)
          setTimeout(() => {
            store.commit(SET_EXIST_INFO, true)
          }, 500)
          globalConfig.setlocalStorage(Vue.prototype.companyUnique, res.data)
          setTimeout(() => {
            window.location.href = `${globalConfig.requireUrl}goodslist_detail?id=${id}&productDetail=1`
          }, 500)
          return
        }
      })
  })
}


export default globalConfig
